<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态文字弹幕</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: white;
            min-height: 100vh;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            width: 100%;
        }
        
        header {
            text-align: center;
            padding: 20px 0;
            margin-bottom: 30px;
        }
        
        h1 {
            font-size: 2.8rem;
            margin-bottom: 10px;
            text-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
            max-width: 600px;
            margin: 0 auto;
        }
        
        .danmu-container {
            position: relative;
            height: 60vh;
            background-color: rgba(0, 0, 0, 0.6);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
            margin-bottom: 30px;
        }
        
        .control-panel {
            background-color: rgba(255, 255, 255, 0.1);
            padding: 20px;
            border-radius: 12px;
            backdrop-filter: blur(10px);
            margin-bottom: 20px;
        }
        
        .input-group {
            display: flex;
            gap: 15px;
            margin-bottom: 15px;
        }
        
        input, select, button {
            padding: 12px 15px;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
        }
        
        input {
            flex: 1;
            background-color: rgba(255, 255, 255, 0.9);
            color: #333;
        }
        
        select {
            background-color: rgba(255, 255, 255, 0.9);
            color: #333;
            cursor: pointer;
        }
        
        button {
            background: linear-gradient(to right, #ff416c, #ff4b2b);
            color: white;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            min-width: 120px;
        }
        
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        
        .color-options {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 15px;
        }
        
        .color-option {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid transparent;
            transition: all 0.2s ease;
        }
        
        .color-option.active {
            border-color: white;
            transform: scale(1.2);
        }
        
        .instructions {
            background-color: rgba(255, 255, 255, 0.1);
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        
        .danmu {
            position: absolute;
            white-space: nowrap;
            user-select: none;
            pointer-events: auto;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);
            font-weight: bold;
            transition: opacity 0.3s;
        }
        
        .danmu:hover {
            opacity: 0.7;
        }
        
        footer {
            text-align: center;
            margin-top: auto;
            padding: 20px;
            font-size: 0.9rem;
            opacity: 0.8;
        }
        
        @media (max-width: 768px) {
            .input-group {
                flex-direction: column;
            }
            
            h1 {
                font-size: 2.2rem;
            }
            
            .danmu-container {
                height: 50vh;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>动态文字弹幕</h1>
            <p class="subtitle">输入您的文字，点击发送，即可看到弹幕效果！尝试悬停在弹幕上暂停它们</p>
        </header>
        
        <div class="control-panel">
            <div class="input-group">
                <input type="text" id="danmuInput" placeholder="输入弹幕内容..." maxlength="50">
                <select id="fontSize">
                    <option value="20px">小字体</option>
                    <option value="28px" selected>标准字体</option>
                    <option value="36px">大字体</option>
                    <option value="44px">超大字体</option>
                </select>
                <button id="sendBtn">发送弹幕</button>
            </div>
            
            <div class="color-options">
                <div class="color-option active" style="background-color: #FF5252;" data-color="#FF5252"></div>
                <div class="color-option" style="background-color: #FF4081;" data-color="#FF4081"></div>
                <div class="color-option" style="background-color: #E040FB;" data-color="#E040FB"></div>
                <div class="color-option" style="background-color: #7C4DFF;" data-color="#7C4DFF"></div>
                <div class="color-option" style="background-color: #536DFE;" data-color="#536DFE"></div>
                <div class="color-option" style="background-color: #448AFF;" data-color="#448AFF"></div>
                <div class="color-option" style="background-color: #40C4FF;" data-color="#40C4FF"></div>
                <div class="color-option" style="background-color: #18FFFF;" data-color="#18FFFF"></div>
                <div class="color-option" style="background-color: #64FFDA;" data-color="#64FFDA"></div>
                <div class="color-option" style="background-color: #69F0AE;" data-color="#69F0AE"></div>
                <div class="color-option" style="background-color: #B2FF59;" data-color="#B2FF59"></div>
                <div class="color-option" style="background-color: #EEFF41;" data-color="#EEFF41"></div>
                <div class="color-option" style="background-color: #FFFF00;" data-color="#FFFF00"></div>
                <div class="color-option" style="background-color: #FFEB3B;" data-color="#FFEB3B"></div>
                <div class="color-option" style="background-color: #FFC107;" data-color="#FFC107"></div>
            </div>
            
            <div class="instructions">
                <p>������ 使用提示：可以选择字体大小和颜色，发送后弹幕会从右侧出现。弹幕速度随机生成，鼠标悬停在弹幕上可以暂停。</p>
            </div>
        </div>
        
        <div class="danmu-container" id="danmuContainer"></div>
    </div>
    
    <footer>
        <p>© 2023 动态文字弹幕 | 使用CSS和JavaScript实现</p>
    </footer>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const danmuContainer = document.getElementById('danmuContainer');
            const danmuInput = document.getElementById('danmuInput');
            const fontSizeSelect = document.getElementById('fontSize');
            const sendBtn = document.getElementById('sendBtn');
            const colorOptions = document.querySelectorAll('.color-option');
            
            let selectedColor = '#FF5252';
            let danmuCount = 0;
            
            // 颜色选择
            colorOptions.forEach(option => {
                option.addEventListener('click', function() {
                    colorOptions.forEach(opt => opt.classList.remove('active'));
                    this.classList.add('active');
                    selectedColor = this.getAttribute('data-color');
                });
            });
            
            // 发送弹幕
            function sendDanmu() {
                const text = danmuInput.value.trim();
                if (!text) return;
                
                // 创建弹幕元素
                const danmu = document.createElement('div');
                danmu.className = 'danmu';
                danmu.textContent = text;
                danmu.style.color = selectedColor;
                danmu.style.fontSize = fontSizeSelect.value;
                
                // 随机设置弹幕的垂直位置
                const containerHeight = danmuContainer.offsetHeight;
                const danmuHeight = parseInt(getComputedStyle(danmu).fontSize) + 10;
                const top = Math.random() * (containerHeight - danmuHeight);
                danmu.style.top = `${top}px`;
                
                // 设置动画
                const duration = 10 + Math.random() * 20; // 随机速度
                danmu.style.animation = `moveLeft ${duration}s linear forwards`;
                
                // 添加悬停暂停效果
                danmu.addEventListener('mouseenter', function() {
                    this.style.animationPlayState = 'paused';
                });
                
                danmu.addEventListener('mouseleave', function() {
                    this.style.animationPlayState = 'running';
                });
                
                // 添加到容器
                danmuContainer.appendChild(danmu);
                
                // 动画结束后移除元素
                danmu.addEventListener('animationend', function() {
                    danmuContainer.removeChild(danmu);
                });
                
                // 清空输入框
                danmuInput.value = '';
                danmuInput.focus();
                
                // 更新计数
                danmuCount++;
            }
            
            // 发送按钮点击事件
            sendBtn.addEventListener('click', sendDanmu);
            
            // 按Enter键发送
            danmuInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendDanmu();
                }
            });
            
            // 定义动画
            const styleSheet = document.createElement('style');
            styleSheet.textContent = `
                @keyframes moveLeft {
                    from { transform: translateX(100%); }
                    to { transform: translateX(-100%); }
                }
            `;
            document.head.appendChild(styleSheet);
            
            // 初始聚焦到输入框
            danmuInput.focus();
            
            // 添加一些示例弹幕
            const sampleTexts = [
                "Hello World!",
                "这是一个弹幕效果~",
                "CSS3动画真强大",
                "JavaScript控制动画",
                "欢迎使用弹幕系统",
                "试试鼠标悬停效果",
                "可以改变颜色和大小",
                "快来发送你的弹幕吧！"
            ];
            
            // 初始化时添加一些示例弹幕
            setTimeout(() => {
                sampleTexts.forEach((text, index) => {
                    setTimeout(() => {
                        danmuInput.value = text;
                        // 随机选择颜色
                        const randomColor = colorOptions[Math.floor(Math.random() * colorOptions.length)];
                        randomColor.click();
                        sendDanmu();
                    }, index * 800);
                });
            }, 1000);
        });
    </script>
</body>
</html>